import Vue from 'vue';
import Vuex from 'vuex';
import { getItem, setItem } from '@/utils/storage';

Vue.use(Vuex);

const USER_DATA = 'userData';

export default new Vuex.Store({
    state: {
        user: getItem(USER_DATA),
        cachePage: []
    },
    mutations: {
        setUser (state, data) {
            state.user = data;
        },
        setEmptyCache (state) {
            state.cachePage = [];
        },
        defaultCache (state) {
            const defaultPage = ['home', 'userIndex', 'qa', 'videoPage'];
            state.cachePage = defaultPage;
        }
    },
    actions: {
        updateUser ({ commit }, obj) {
            commit('setUser', obj);
            setItem(USER_DATA, obj); // 登入信息持久化
        },
        emptyCache ({ commit }) {
            commit('setEmptyCache');
        },
        defaultCache ({ commit }) {
            commit('defaultCache');
        }
    },
    modules: {
    }
});
